#pragma once

#ifdef WGLENGINE_DLL
#define WGLENGINE_EXPORT __declspec(dllexport)
#else
#define WGLENGINE_EXPORT __declspec(dllimport)
#endif

#include "GL\glew.h"
#include "glm\glm.hpp"

#include "Material.h"
#include "SceneMesh.h"
#include "SceneNode.h"

#include <vector>

class WGLENGINE_EXPORT WQuad : public SceneMesh
{
protected:
	virtual void RenderMesh(int pass) override
	{
		std::shared_ptr<Shader> shader = mat->GetShader();
		mat->Bind();
		glBindVertexArray(vaoID);
		glDrawArrays(GL_TRIANGLES, 0, points.size());
		glBindVertexArray(0);
		mat->Unbind();
	}

	std::vector<glm::vec3> points;
	std::vector<glm::vec2> texcoords;

public:
	WQuad(std::vector<glm::vec3> pts, std::vector<glm::vec2> uvs, std::string material = "");
	~WQuad();

	virtual void GenVBO() override
	{
		CreateVertexArray(points.size());
		for(int i=0; i<points.size(); i++)
		{
			SetVertexArray(i*3, points.at(i).x);
			SetVertexArray(i*3 + 1, points.at(i).y);
			SetVertexArray(i*3 + 2, points.at(i).z);

			SetUVArray(i*2, texcoords.at(i).x);
			SetUVArray(i*2 + 1, texcoords.at(i).y);
		}


		vboIDs = std::unique_ptr<GLuint[]>(new GLuint[3]);

		glGenVertexArrays(1, &vaoID);
		glBindVertexArray(vaoID);

		glGenBuffers(3, vboIDs.get());

		glEnableVertexAttribArray(0);
		glBindBuffer(GL_ARRAY_BUFFER, vboIDs[0]);
		glBufferData(GL_ARRAY_BUFFER, points.size() * 3 * sizeof(GL_FLOAT), vertexPosArray.get(), GL_STATIC_DRAW);
		glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (GLubyte *) nullptr);
			
		glEnableVertexAttribArray(1);
		glBindBuffer(GL_ARRAY_BUFFER, vboIDs[1]);
		glBufferData(GL_ARRAY_BUFFER, points.size() * 1 * sizeof(unsigned int), vertexDrawIDArray.get(), GL_STATIC_DRAW);
		glVertexAttribPointer(1, 1, GL_FLOAT, GL_FALSE, 0, (GLubyte *) nullptr);

		glEnableVertexAttribArray(2);
		glBindBuffer(GL_ARRAY_BUFFER, vboIDs[2]);
		glBufferData(GL_ARRAY_BUFFER, points.size() * 2 * sizeof(GL_FLOAT), vertexUVArray.get(), GL_STATIC_DRAW);
		glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, (GLubyte *) nullptr);
	
		glBindVertexArray(0);
		initiated = true;
	}
};